//
//  HNMainViewController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2016/12/13.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNMainViewController.h"
#import "HNDoctorListViewController.h"
#import "HNDoctorDetailViewController.h"
#import "HNWebViewController.h"
#import "HNSearchViewController.h"

@interface HNMainViewController ()<UIScrollViewDelegate>{
    BOOL isFromStart;
}

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

//contentScrollView的高度
@property (nonatomic,assign)CGFloat contentHeight;

//轮播图变量
@property(nonatomic, strong)UIScrollView  *scrollView;      //声明一个UIScrollView
@property(nonatomic, strong)UIPageControl *pageControl;     //声明一个UIPageControl
@property(nonatomic, strong)NSMutableArray  *arrayImages;          //存放图片的数组
@property(nonatomic, strong)NSMutableArray *viewController; //存放UIViewController的可变数组
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,strong)UIView *rxView;



@property (nonatomic,strong)NSMutableArray *docterListSource;
@property (nonatomic,assign)NSInteger docterCount ;
@property (nonatomic,strong)UIView *shoppingView;//商品详情View
@property (nonatomic,assign)CGFloat *shoppingHeight;//商品详情高度


@property (nonatomic,assign) int cur;

@end

@implementation HNMainViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //在其他离开改页面的方法同样加上下面代码
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
      [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)drawView{
//    _contentScrollView.backgroundColor = RGBJ(248);
    
//    self.contentScrollView = 
    _contentScrollView.delegate = self;
    _contentScrollView.bounces = YES;
    _docterListSource = [[NSMutableArray alloc]init];
    _docterCount = 0;
    
    
    MJRefreshGifHeader *header = [[MJRefreshGifHeader alloc]init];
    
    [header setRefreshingBlock:^(void){
        [self getAdsImage];
    }];
    self.contentScrollView.mj_header = header;
    
    //    //设置轮播图
    [self setAdsVC:nil];
    //
    //
    //    //设置搜索
    [self setSearchBtn];

    
}


-(void)initData{
    //获取轮播图数据
    
    
    self.arrayImages = [[NSMutableArray alloc]init];
    [self getAdsImage];
    
    self.cur = 1;//分页初始赋值
    
    
    [self getUserInfo];
    
    //加载可是列表
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    [param setObject:@"department" forKey:@"flag1"];
    NSLog(@"param = %@",param);
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_choice_value parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取医生列表失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSArray * choiceListArr = [data objectForKey:@"obj"];

            [Util addDepartmentArr:choiceListArr];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];

        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取医生列表失败"];

    }];

 
}


-(void)setAdsVC:(NSMutableArray *)imageArr{
    _contentHeight = 0;
    if (!imageArr) {
//       self.arrayImages = [NSMutableArray arrayWithObjects:@"ads_1.png",@"54521417567978.png", nil];
    }else{
        self.arrayImages = imageArr;
    }
    
    self.adsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Width*3/5)];
    _contentHeight = _contentHeight + self.adsView.frame.size.height;
    _contentScrollView.contentSize = CGSizeMake(kScreen_Width,_contentHeight);
    [self.contentScrollView addSubview:self.adsView];
    CGFloat height = self.adsView.frame.size.height;
    CGFloat width = self.adsView.frame.size.width;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
    [_scrollView setPagingEnabled:YES];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [_scrollView setDelegate:self];
    [_scrollView setBackgroundColor:[UIColor lightGrayColor]];
    
    //ContentSize 这个属性对于UIScrollView挺关键的，取决于是否滚动。
    [_scrollView setContentSize:CGSizeMake(width * [self.arrayImages count], height)];
    [self.adsView addSubview:self.scrollView];
    
    [self.scrollView setBounces:YES];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width/2-80, self.scrollView.frame.size.height - 20, 160, 20)];
    //    [_pageControl setBackgroundColor:[UIColor blackColor]];
    
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = [self.arrayImages count];
    [_pageControl addTarget:self action:@selector(chagePage:) forControlEvents:UIControlEventValueChanged];
    [self.adsView addSubview:self.pageControl];
    
    _viewController = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < [self.arrayImages count]; i++) {
        [_viewController addObject:[NSNull null]];
    }
    
    //加载每张图
    for (NSInteger i = 0; i<self.arrayImages.count; i++) {
        [self loadScrollViewPage:i];
    }
    
//    _timer = [NSTimer scheduledTimerWithTimeInterval:self.arrayImages.count*2 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];

    
}

-(void)setSearchBtn{
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, kScreen_Width, 70)];
    searchView.backgroundColor = [UIColor whiteColor];
    _contentHeight = _contentHeight + searchView.frame.size.height;
    
    NSLog(@"%f",_contentHeight);
    
    _contentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
    [self.contentScrollView addSubview:searchView];
    
    CGFloat gl = 15;
    CGFloat bl = 10;
    
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(bl, gl, kScreen_Width-3*bl - 60, 40)];
    leftView.clipsToBounds = YES;
    leftView.layer.borderWidth = 1;
    leftView.layer.borderColor = (__bridge CGColorRef )RGB(183, 183, 183);
    leftView.layer.cornerRadius = 4;
    leftView.backgroundColor = RGB(249, 249, 249);
    [searchView addSubview:leftView];
    
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,10,100,20)];
    textLabel.numberOfLines = 1;
    textLabel.textAlignment = NSTextAlignmentLeft;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = RGBJ(189);
    textLabel.text = @"请输入关键字";
    [leftView addSubview:textLabel];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreen_Width-60 - bl,gl,60,40)];
    nameLabel.numberOfLines = 1;
    nameLabel.clipsToBounds = YES;
    nameLabel.layer.cornerRadius = 4;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.backgroundColor = RGB(85, 206, 84);
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.text = @"搜索";
    [searchView addSubview:nameLabel];
    
    UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [clickBtn setTitle:@"" forState:UIControlStateNormal];
    [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
    [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    clickBtn.frame = searchView.frame;//大小和位置
    [clickBtn addTarget:self action:@selector(enterSearch:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentScrollView addSubview:clickBtn];
    
}



#pragma mark - 进入搜索页面
-(void)enterSearch:(UIButton *)btn{
    HNSearchViewController *webVC = [[HNSearchViewController alloc]init];
    [self.navigationController pushViewController:webVC animated:YES];
}


#pragma -mark 创建列表页
-(void)setDoctorList{
    
    
    if (self.docterListSource.count == 0) {
        return;
    }
    
    
    [self.shoppingView removeFromSuperview];
    
    self.shoppingView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, kScreen_Width, 0)];
    
    NSLog(@"%f",_contentHeight);
    
    [self.contentScrollView addSubview:_shoppingView];
    
    CGFloat bl = 6;//border边距
    CGFloat gl = 4;//gap间隙
    
    NSInteger num = self.docterListSource.count;
    //当商品列表为空时

    
    for (int n = 0; n < 1; n++) {
        if (self.docterCount == 0) {
            //头部行
            UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, self.shoppingView.frame.size.height, kScreen_Width, 40)];
            [_shoppingView addSubview:headView];
            
            UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 20, 30)];
            signImageView.image = [UIImage imageNamed:@"千库网-热门@2x.png"];
            [headView addSubview:signImageView];
            
            //标题
            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45,10,150,20)];
            nameLabel.numberOfLines = 1;
            nameLabel.clipsToBounds = YES;
            nameLabel.textAlignment = NSTextAlignmentLeft;
            nameLabel.font = [UIFont systemFontOfSize:15];
            
            nameLabel.textColor = RGBJ(110);
            nameLabel.text = @"热门名医";
            [headView addSubview:nameLabel];
            
            UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreen_Width - 40, 10, 20, 20)];
            rightImageView.image = [UIImage imageNamed:@"rightBtn.png"];
            [headView addSubview:rightImageView];
            
            //添加点击按钮——覆盖在原来的View上
            UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [clickBtn setTitle:@"" forState:UIControlStateNormal];
            [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
            [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            clickBtn.frame = headView.frame;//大小和位置
            [clickBtn addTarget:self action:@selector(enterDoctorList:) forControlEvents:UIControlEventTouchUpInside];
            [_shoppingView addSubview:clickBtn];
            
            
            
            _contentHeight = _contentHeight + headView.frame.size.height;
            _contentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
            
            //设置商品高度
            CGRect shoppingViewFrame = self.shoppingView.frame;
            shoppingViewFrame.size.height = shoppingViewFrame.size.height + headView.frame.size.height;
            self.shoppingView.frame = shoppingViewFrame;
        }
        
        for (NSInteger i = self.docterCount; i < self.docterListSource.count; i++) {
            NSDictionary *goodsDataDic  = self.docterListSource[i];
            UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake((kScreen_Width/2 - bl/2) * !(i%2 == 0) + bl, self.shoppingView.frame.size.height, kScreen_Width/2, 0)];
            [self.shoppingView addSubview:itemView];
            //商品图
            UIImageView *goodsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, gl * 2, kScreen_Width/2 - bl*3/2, kScreen_Width/2 - bl*3/2)];
            
            NSString *goodsImageStr = [NSString stringWithFormat:@"%@%@",url_Base,[goodsDataDic objectForKey:@"photo"]];
            [goodsImageView sd_setImageWithURL:[NSURL URLWithString:goodsImageStr] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F@2x.png"]];
            
            [itemView addSubview:goodsImageView];
            
            //商品名
            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(gl, goodsImageView.frame.origin.y + goodsImageView.frame.size.height + gl, goodsImageView.frame.size.width - 2*gl, 18)];
            nameLabel.numberOfLines = 1;
            nameLabel.font = [UIFont systemFontOfSize:15];
            nameLabel.textColor = RGBJ(165);
            NSString *realName = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"realName"]];
            NSString *nickname = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"nickname"]];
             NSString *username = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"username"]];
            if ([AppConfig isNULL:nickname]) {
                if ([AppConfig isNULL:realName]){
                    nameLabel.text = username;
                }else{
                    nameLabel.text = realName;
                }
            }else {
                nameLabel.text = nickname;
            }
            
            
            [itemView addSubview:nameLabel];
            //商品描述
            UILabel *detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(gl, nameLabel.frame.origin.y + nameLabel.frame.size.height + gl, goodsImageView.frame.size.width - 2*gl, 15)];
            detailLabel.numberOfLines = 1;
            detailLabel.font = [UIFont systemFontOfSize:11];
            detailLabel.textColor = RGB(112, 112, 112);
            detailLabel.text = [NSString stringWithFormat:@" %@",[goodsDataDic objectForKey:@"flag1"]];
            [itemView addSubview:detailLabel];
            
            //添加点击按钮
            UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [clickBtn setTitle:@"" forState:UIControlStateNormal];
            [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
            [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            clickBtn.tag = i + 10000;
            clickBtn.frame = goodsImageView.frame;
            [clickBtn addTarget:self action:@selector(enterDoctorDetail:) forControlEvents:UIControlEventTouchUpInside];
            [itemView addSubview:clickBtn];
            
            //设置列表高度
            CGRect itemViewFrame = itemView.frame;
            itemViewFrame.size.height = itemViewFrame.size.height + 10 + 8*gl + goodsImageView.frame.size.height + nameLabel.frame.size.height;
            itemView.frame = itemViewFrame;
            if (i%2 == 1 || (i + 1 == num && num%2 == 1)) {
                //设置列表高度
                _contentHeight = _contentHeight + itemView.frame.size.height;
                //最后一行加载后再改变scrollView高度
                if(i + 1 == num){
                    _contentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
                }
                //设置商品高度
                CGRect shoppingViewFrame = self.shoppingView.frame;
                shoppingViewFrame.size.height = shoppingViewFrame.size.height + itemView.frame.size.height;
                self.shoppingView.frame = shoppingViewFrame;
            }
        }

    }
    self.docterCount = self.docterListSource.count;
}

#pragma -mark 进入医生列表页
-(void)enterDoctorList:(UIButton *)btn{
    HNDoctorListViewController *doctorListVC = [[HNDoctorListViewController alloc]init];
    doctorListVC.title = @"热门名医";
    [self.navigationController pushViewController:doctorListVC animated:YES];
    
}

#pragma -mark 进入医生详情页
-(void)enterDoctorDetail:(UIButton *)btn{
    NSInteger tag = btn.tag - 10000;
    NSDictionary *goodsDataDic  = self.docterListSource[tag];
    NSLog(@"%@",goodsDataDic);
    
    HNDoctorDetailViewController *doctorDetailVC = [[HNDoctorDetailViewController alloc]init];
    doctorDetailVC.title = @"医生详情";
    
    
    doctorDetailVC.userId = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"userId"]];
    
    
    [self.navigationController pushViewController:doctorDetailVC animated:YES];
}





#pragma mark - 轮播图展现
-(void)loadScrollViewPage:(NSInteger)page
{
    if (page >= self.arrayImages.count) {
        return;
    }
    
    UIImageView *adsImageView = [self.viewController objectAtIndex:page];
    if ((NSNull *)adsImageView == [NSNull null])
    {
        adsImageView = [[UIImageView alloc] init];
        [self.viewController replaceObjectAtIndex:page withObject:adsImageView];
    }
    
    if (adsImageView.superview == nil) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = CGRectGetWidth(frame) * page;
        frame.origin.y = 0;
        adsImageView.frame = frame;
        
        [self.scrollView addSubview:adsImageView];
        
        NSLog(@"%@",self.arrayImages);
        
        if (self.arrayImages) {
            NSString *imageStr = [NSString stringWithFormat:@"%@%@",url_Base,[[self.arrayImages objectAtIndex:page] objectForKey:@"advertImg"]];
            NSLog(@"%@",imageStr);
            
             [adsImageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"轮播.png"]];
        }
        

    }
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    NSInteger page = floor((self.scrollView.contentOffset.x -pageWidth/2)/pageWidth) ;
    if (self.scrollView.contentOffset.x<0) {
        _pageControl.currentPage = self.arrayImages.count - 1;
    }else if(self.scrollView.contentOffset.x > (self.arrayImages.count - 1)*kScreen_Width){
        _pageControl.currentPage = 0;
    }else{
        _pageControl.currentPage = page + 1;
    }
    [self loadScrollViewPage:page];
    //    [self loadScrollViewPage:page-1];
    //    [self loadScrollViewPage:page];
    //    [self loadScrollViewPage:page+1];
}

//- (IBAction)changePage:(id)sender
//{
//    NSInteger page = self.pageControl.currentPage;
//
//    [self loadScrollViewPage:page - 1];
//    [self loadScrollViewPage:page];
//    [self loadScrollViewPage:page + 1];

//    CGRect bounds = self.scrollView.bounds;
//    bounds.origin.x = Kwidth * page - 1;
//    bounds.origin.y = 0;
//    [self.scrollView scrollRectToVisible:bounds animated:YES];
//}


//NSTimer
-(void)scrollPages{
    //    ++self.pageControl.currentPage;
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    if (isFromStart) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        _pageControl.currentPage = 0;
    }
    else
    {
        _pageControl.currentPage ++;
        
        [self.scrollView setContentOffset:CGPointMake(pageWidth*_pageControl.currentPage, self.scrollView.bounds.origin.y)];
        
        [self loadScrollViewPage:_pageControl.currentPage];
    }
    if (_pageControl.currentPage == _pageControl.numberOfPages - 1) {
        isFromStart = YES;
    }
    else
    {
        isFromStart = NO;
    }
}

#pragma mark - 轮播图 UIPageControl
-(void)chagePage:(UIPageControl *)pageControl
{
    //    _pageControl.currentPage
}


#pragma mark - scrollViewDelegate
// contentScrollView 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    CGFloat offset = _noListNormalH - 58 - 36 - 36;
//    CGFloat contentScrolloffectY = _contentScrollView.contentOffset.y;
//    if (_noListNormalH != 0) {
//        if (contentScrolloffectY >= offset) {
//            self.srarchView.alpha = 1;
//            [self.listHeadView setHidden:YES];
//            [self.listOtherHeadView setHidden:NO];
//            [self.searchBackground setBackgroundColor:RGB(244, 244, 244)];
//        }else{
//            self.srarchView.alpha = contentScrolloffectY/offset;
//            [self.listHeadView setHidden:NO];
//            [self.listOtherHeadView setHidden:YES];
//            [self.searchBackground setBackgroundColor:RGB(255, 255, 255)];
//        }
//    }
    
    //    NSLog(@"scrollViewDidScroll");
}

// contentScrollView 开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //    NSLog(@"scrollViewWillBeginDragging");
}

// contentScrollView 结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //    NSLog(@"scrollViewDidEndDragging");
}


#pragma mark - 初始化数据--轮播图

-(void)getAdsImage{
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_Advert_list parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取轮播图失败"];
             NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSMutableArray * obj = [NSMutableArray arrayWithArray:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",obj);
            
            _timer = [NSTimer scheduledTimerWithTimeInterval:obj.count*2 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];
            
            [_adsView removeFromSuperview];
            
            
            
            //    //设置轮播图
            [self setAdsVC:obj];
            //
            //
            //    //设置搜索
            [self setSearchBtn];
        

            //过去列表信息
             [self getDoctorData];
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
            [self.contentScrollView.mj_header endRefreshing];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
      [self showHint:@"获取轮播图失败"];
    [self.contentScrollView.mj_header endRefreshing];
    }];
    

}


-(void)getDoctorData{
    
  
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:[NSString stringWithFormat:@"%d",_cur] forKey:@"cur"];
    [param setObject:@"1" forKey:@"isDoctor"];
    NSLog(@"param = %@",param);
    
    
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_doctor_list parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取热门医生列表失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSArray * obj = [NSArray arrayWithArray:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",obj);
            
            for (int i = 0; i < obj.count; i ++) {
                NSLog(@"%@",obj[i]);
                [self.docterListSource addObject:obj[i]];
            }
            
            NSLog(@"===self.docterListSource = %@",self.docterListSource);
            
            
            
             [self setDoctorList];
            
                [self.contentScrollView.mj_header endRefreshing];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
                [self.contentScrollView.mj_header endRefreshing];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取热门医生列表失败"];
            [self.contentScrollView.mj_header endRefreshing];
    }];
    
}


-(void)getUserInfo{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_userInfo_detail parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取用户信息失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",userInfo);
            
            
            NSString *userImageStr = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"photo"]];
            if ([AppConfig isNULL:userImageStr]) {
                userImageStr = @"null";
            }
            NSString *userNickname = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"nickname"]];
            if ([AppConfig isNULL:userNickname]) {
                userImageStr = @"未设置";
            }
            NSString *userUserName = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"username"]];
            if ([AppConfig isNULL:userUserName]) {
                userImageStr = @"未设置";
            }
            NSDictionary *saveDic = [NSDictionary dictionaryWithObjectsAndKeys:userImageStr,@"photo",userUserName,@"hnLoginName",userNickname,@"nickname", nil];
            
            [AppConfig saveUserInfo:saveDic];
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取用户信息失败"];
    }];
    
    
}


@end
